﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using He.Web.UI;
using He.Common;

namespace He.Web.manage.logistics
{
    public partial class edit : Manage
    {
        public string action = Enums.ActionEnum.Add.ToString(); //操作类型
        public int id;
        public string pos_mark;
        protected void Page_Load(object sender, EventArgs e)
        {
            pos_mark = GetString("pos_mark");
            string _action = GetString("action");
            if (!string.IsNullOrEmpty(_action) && _action == Enums.ActionEnum.Edit.ToString())
            {
                this.action = Enums.ActionEnum.Edit.ToString();//修改类型
                if (!int.TryParse(Request.QueryString["id"] as string, out this.id))
                {
                    ShowParamError();
                    return;
                }
            }

            if (!IsPostBack)
            {
                ddlStatus.DataSource = EnumHelper.GetList(typeof(Enums.LogisticsStatus));
                ddlStatus.DataTextField = "Text";
                ddlStatus.DataValueField = "Value";
                ddlStatus.DataBind();

                if (action == Enums.ActionEnum.Edit.ToString())
                {
                    chkLoginLevel("edit" + pos_mark);

                    ShowInfo(this.id);
                }
                else
                {
                    chkLoginLevel("add" + pos_mark);
                }
            }
        }
        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            Model.logistics model = new BLL.logistics().GetModel(_id);
            lbOrderNumber.Text = model.order_number;
            ddlStatus.SelectedValue = model.status.ToString();
            txtSenderName.Text = model.sender_name;
            txtSenderTel.Text = model.sender_tel;
            txtSenderPhone.Text = model.sender_phone;
            txtSenderZipcode.Text = model.sender_zipcode;
            txtSenderAddress.Text = model.sender_address;
            txtReceiverName.Text = model.receiver_name;
            txtReceiverTel.Text = model.receiver_tel;
            txtReceiverPhone.Text = model.receiver_phone;
            txtReceiverZipcode.Text = model.receiver_zipcode;
            txtReceiverAddress.Text = model.receiver_address;
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            BLL.logistics bll = new BLL.logistics();
            Model.logistics model = new Model.logistics();

            if (bll.ExistsOrderNumber(txtOrderNumber.Text.Trim()))
            {
                ShowError("该订单号已存在，请勿重复输入");
                return false; ;
            }

            model.order_number = txtOrderNumber.Text.Trim();
            model.status =int.Parse(ddlStatus.SelectedValue);
            model.sender_name = txtSenderName.Text.Trim();
            model.sender_tel = txtSenderTel.Text.Trim();
            model.sender_phone = txtSenderPhone.Text.Trim();
            model.sender_zipcode = txtSenderZipcode.Text.Trim();
            model.sender_address = txtSenderAddress.Text.Trim();

            model.receiver_name = txtReceiverName.Text.Trim();
            model.receiver_tel = txtReceiverTel.Text.Trim();
            model.receiver_phone = txtReceiverPhone.Text.Trim();
            model.receiver_zipcode = txtReceiverZipcode.Text.Trim();
            model.receiver_address = txtReceiverAddress.Text.Trim();

            model.create_time = DateTime.Now.ToString();
            if (bll.Add(model) > 0)
            {
                SaveLogs(string.Format("[物流管理]增加物流单号:{0}", model.order_number));
                return true;
            }
            return false;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            BLL.logistics bll = new BLL.logistics();
            Model.logistics model = new BLL.logistics().GetModel(_id);

            model.status = int.Parse(ddlStatus.SelectedValue);

            model.sender_name = txtSenderName.Text.Trim();
            model.sender_tel = txtSenderTel.Text.Trim();
            model.sender_phone = txtSenderPhone.Text.Trim();
            model.sender_zipcode = txtSenderZipcode.Text.Trim();
            model.sender_address = txtSenderAddress.Text.Trim();

            model.receiver_name = txtReceiverName.Text.Trim();
            model.receiver_tel = txtReceiverTel.Text.Trim();
            model.receiver_phone = txtReceiverPhone.Text.Trim();
            model.receiver_zipcode = txtReceiverZipcode.Text.Trim();
            model.receiver_address = txtReceiverAddress.Text.Trim();

            if (bll.Update(model))
            {
                SaveLogs(string.Format("[物流管理]修改物流单号:{0}", model.order_number));
                return true;
            }
            return false;
        }
        #endregion

        protected void btnSave_Click(object sender, EventArgs e)
        {

            if (action == Enums.ActionEnum.Edit.ToString()) //修改
            {
                string url = CookieHelper.Get("LastListUrl");
                if (string.IsNullOrEmpty(url))
                {
                    url = Request.RawUrl;
                }

                if (!DoEdit(this.id))
                {
                    JscriptPrint("保存过程中发生错误啦！", url, "Error");
                    return;
                }
                JscriptPrint("修改信息成功啦！", url, "Success");
            }
            else //添加
            {
                if (!DoAdd())
                {
                    JscriptPrint("保存过程中发生错误啦！", Request.RawUrl, "Error");
                    return;
                }
                JscriptPrint("添加信息成功啦！", Request.RawUrl, "Success");
            }
        }
    }
}